package com.yanqu.road.server.manger.maidian.cricket;

import com.yanqu.data.analytics.constant.EventDataConstants;
import com.yanqu.data.analytics.helper.DataBuilder;
import com.yanqu.road.entity.config.patrons.PatronsInfo;
import com.yanqu.road.entity.config.skill.SkillInfo;
import com.yanqu.road.entity.cricket.CricketData;
import com.yanqu.road.entity.cricket.CricketEquipSkill;
import com.yanqu.road.entity.cricket.CricketSkill;
import com.yanqu.road.entity.cricket.CricketTalent;
import com.yanqu.road.entity.cricket.config.CricketConfig;
import com.yanqu.road.entity.enums.eSkillType;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.entity.player.UserPatronsSkill;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.cricket.CricketMgr;
import com.yanqu.road.server.gameplayer.module.cricket.CricketModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.manger.DataAnalyticsMgr;
import com.yanqu.road.server.manger.config.PatronsMgr;
import com.yanqu.road.server.manger.config.SkillMgr;
import com.yanqu.road.server.manger.maidian.DefaultEventTraceAction;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CricketLog {

    private static String CRICKET_GET = "cricket_get";
    private static String CRICKET_GETPLACE = "cricket_getplace";
    private static String CRICKET_GETTYPE = "cricket_gettype";
    private static String CRICKET_GETCONTENT = "cricket_getcontent";
    private static String CRICKET_ID = "cricket_id";
    private static String CRICKET_QUALITY = "cricket_quality";



    /**
     * 获得蛐蛐
     * @param player 玩家
     * @param cricketDataList 蛐蛐列表
     * @param type 类型
     * @param scenceId 场景
     */
    public static void trackCricketGet(GamePlayer player, List<CricketData> cricketDataList, int type, int scenceId){

        try{

            if(!DataAnalyticsMgr.openDataAnalytics()){
                return;
            }

            DefaultEventTraceAction action = new DefaultEventTraceAction(CRICKET_GET, player);
            action.build(CRICKET_GETPLACE, scenceId);
            action.build(CRICKET_GETTYPE, type);
            List<Map<String, Object>> group = new ArrayList<>();
            for (CricketData cricketData : cricketDataList) {
                Map<String, Object> map = new HashMap<>();
                map.put(CRICKET_ID, cricketData.getCricketId());
                map.put(CRICKET_QUALITY, cricketData.getQuality());
                group.add(map);
            }
            action.build(CRICKET_GETCONTENT, group);
            action.trace();
        }catch (Exception e){
            DataAnalyticsMgr.getLogger().error("error ", e);
        }
    }


    //=========================

    private static String CRICKET = "cricket";

    private static String CRICKET_NAME = "cricket_name";
    private static String CRICKET_LV = "cricket_lv";
    private static String CRICKET_VOCATION = "cricket_vocation";
    private static String CRICKET_QUELITY = "cricket_quelity";
    private static String CRICKET_INTELLIGENCE = "cricket_intelligence";
    private static String CRICKET_NUMBER = "cricket_number";
    private static String CRICKET_RANK = "cricket_rank";
    private static String CRICKET_ADD = "cricket_add";
    private static String CRICKET_SKILL = "cricket_skill";
    private static String CRICKET_ADDINTELLIGENCE = "cricket_addintelligence";

    private static String CRICKET_SKILLNAME = "cricket_skillname";
    private static String CRICKET_SKILLSTAR = "cricket_skillstar";
    private static String CRICKET_SKILLISFIRST = "cricket_skillisfirst";

    private static String CRICKET_DRAW = "cricket_draw";
    private static String CRICKET_LEVEL = "cricket_level";
    private static String CRICKET_ROOM = "cricket_room";
    private static String CRICKET_NUM = "cricket_num";

    private static String CRICKET_LIFE = "cricket_life";
    private static String CRICKET_ATTACK = "cricket_attack";
    private static String CRICKET_MANNER = "cricket_manner";

    private static String CRICKET_INFO = "cricket_info";

    public static void uploadCricketBaseInfoSnapshot(GamePlayer player) {

        try {
            if (!DataAnalyticsMgr.openDataAnalytics()) return;

            DefaultEventTraceAction action = new DefaultEventTraceAction(CRICKET_INFO, player);

            CricketModule cricketModule = player.getModule(CricketModule.class);

            action.build(CRICKET_DRAW, 60 * cricketModule.getUserCricketData().getDrawLevel() + cricketModule.getUserCricketData().getDrawPieceCount());
            action.build(CRICKET_LEVEL, cricketModule.getUserCricketData().getBadgeLevel());
            //促织庙
            List<Map<String, Object>> objectGroup = new ArrayList<>();
            for (Map.Entry<Integer, UserPatrons> entry : player.getModule(PatronsModule.class).getUserPatronsMap().entrySet()) {
                UserPatrons userPatrons = entry.getValue();
                for (UserPatronsSkill patronsSkill : userPatrons.getSkillList()) {
                    if(SkillMgr.isPatronsCricketFateSkill(patronsSkill.getSkillId())){
                        Map<String, Object> map = new HashMap<>();
                        map.put(EventDataConstants.Patrons.GUEST_ID, userPatrons.getPatronsId());
                        PatronsInfo info = PatronsMgr.getPatronsInfo(userPatrons.getPatronsId());
                        map.put(EventDataConstants.Patrons.GUEST_VOCATION, info.getOccupation());
                        map.put(CRICKET_NUM, patronsSkill.getSkillLv());
                        objectGroup.add(map);
                    }
                }
            }
            action.build(CRICKET_ROOM, objectGroup);

            action.trace();
        } catch (Exception e) {
            DataAnalyticsMgr.getLogger().error(" error ", e);
        }

    }

    //蛐蛐快照
    public static void trackInfoSnapshot(GamePlayer player) {
        try {
            if (!DataAnalyticsMgr.openDataAnalytics()) return;

            CricketModule cricketModule = player.getModule(CricketModule.class);

            //公共事件属性
            DataBuilder common = DataBuilder.newBuilder(player.getUserId(), DataAnalyticsMgr.getDistinctId(player.getUserInfo()));
            DataAnalyticsMgr.buildPublicEventData(player, common);

            for (Map.Entry<Integer, CricketData> dataEntry : cricketModule.getCricketDataMap().entrySet()) {
                CricketData cricketData = dataEntry.getValue();

                DataBuilder dataBuilder = DataBuilder.newBuilder(player.getUserInfo().getUserId(), DataAnalyticsMgr.getDistinctId(player.getUserInfo()));

                CricketConfig config = CricketMgr.getConfig().getCricketConfig(cricketData.getCricketId());
                if(config == null){
                    continue;
                }

                dataBuilder.build(CRICKET_ID, cricketData.getCricketId());
                dataBuilder.build(CRICKET_LV, cricketData.getTitle());
                dataBuilder.build(CRICKET_VOCATION, config.getOccupation());
                dataBuilder.build(CRICKET_QUELITY, cricketData.getQuality());

                CricketTalent talent = cricketModule.calcCricketTalent(cricketData);
                dataBuilder.build(CRICKET_LIFE, talent.getHp());
                dataBuilder.build(CRICKET_ATTACK, talent.getAtk());
                dataBuilder.build(CRICKET_MANNER, talent.getPower());

                dataBuilder.build("cricket_littletit", cricketData.getLittleTitle());

                //战力
                dataBuilder.build(CRICKET_NUMBER, cricketData.getCricketProperty().getAbility());

                //装备门客
                if(cricketData.getEquipPatronsId() > 0){
                    int qualification = 0;
                    for (CricketEquipSkill skillData : cricketData.getEquipSkillList()) {
                        SkillInfo skillInfo = SkillMgr.getSkillInfo(skillData.getSkillId());
                        if (skillInfo != null && skillInfo.getType() == eSkillType.Qualification.getValue()) {
                            //现在没等级，默认1级
                            qualification += SkillMgr.getSkillAddition(skillInfo, skillData.getLv());
                        }
                    }
                    dataBuilder.build(CRICKET_ADDINTELLIGENCE, qualification);
                }
                //蛐蛐技能
                List<Map<String, Object>> objectGroup = new ArrayList<>();
                for (CricketSkill skill : cricketData.getAllCricketSkillList()) {
                    Map<String, Object> map = new HashMap<>();
                    map.put(CRICKET_SKILLNAME, skill.getSkillId());
                    // 获取星级
                    SkillInfo skillInfo = SkillMgr.getSkillInfo(skill.getSkillId());
                    if (skillInfo != null) {
                        map.put(CRICKET_SKILLSTAR, skillInfo.getStars());
                    }
                    if(cricketData.getInitSkillList().contains(skill)){
                        map.put(CRICKET_SKILLISFIRST, true);
                    }else {
                        map.put(CRICKET_SKILLISFIRST, false);
                    }
                    objectGroup.add(map);
                }
                dataBuilder.build(CRICKET_SKILL, objectGroup);
                dataBuilder.getDataMap().putAll(common.getDataMap());
                DataAnalyticsMgr.provider.track(dataBuilder.getAccountId(), dataBuilder.getDistinctId(), CRICKET, dataBuilder.getDataMap());
            }

        } catch (Exception e) {
            DataAnalyticsMgr.getLogger().error(" error ", e);
        }

    }



}
